package com.mgetool.test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;

public class ss {  
	public static void main(String[] args) {  
		Display display = new Display();  
		Color red = display.getSystemColor(SWT.COLOR_RED);  
		Color blue = display.getSystemColor(SWT.COLOR_BLUE);  
		Shell shell = new Shell(display);  
		shell.setLayout(new FillLayout());  
		// set the size of the scrolled content - method 1  
		final ScrolledComposite sc1 = new ScrolledComposite(shell, SWT.H_SCROLL  
				| SWT.V_SCROLL | SWT.BORDER);  
		final Composite c1 = new Composite(sc1, SWT.NONE);  
		sc1.setContent(c1);  
		c1.setBackground(red);  
		GridLayout layout = new GridLayout();  
		layout.numColumns = 4;  
		c1.setLayout(layout);  
		Button b1 = new Button(c1, SWT.PUSH);  
		b1.setText("first button");  
		/* 
		 * 这是两种用法中的一种，效果是：根据已经存在的组件，计算需要的composite的区域的大小 
		 * 即运行结果中红色显示的部分。 
		 */  
		c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));  

		// set the minimum width and height of the scrolled content - method 2  
//		final ScrolledComposite sc2 = new ScrolledComposite(shell, SWT.H_SCROLL  
//				| SWT.V_SCROLL | SWT.BORDER);  
//		sc2.setExpandHorizontal(true);  
//		sc2.setExpandVertical(true);  
//		final Composite c2 = new Composite(sc2, SWT.NONE);  
//		sc2.setContent(c2);  
//		c2.setBackground(blue);  
//		layout = new GridLayout();  
//		layout.numColumns = 4;  
//		c2.setLayout(layout);  
//		Button b2 = new Button(c2, SWT.PUSH);  
//		b2.setText("first button");  
//		/* 
//		 * 这是第二种用法，整个composite区域是用到的Composite区域 
//		 */  
//		sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
		Button add = new Button(shell, SWT.PUSH);  
		add.setText("add children");  
		final int[] index = new int[] { 0 };  
		add.addListener(SWT.Selection, new Listener() {  
			public void handleEvent(Event e) {  
				index[0]++;  
				Button button = new Button(c1, SWT.PUSH);  
				button.setText("button " + index[0]);  
				// reset size of content so children can be seen - method 1  
				c1.setSize(c1.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
				c1.layout();  
//				button = new Button(c2, SWT.PUSH);  
				button.setText("button " + index[0]);  
				// reset the minimum width and height so children can be seen -  
				// method 2  
//				sc2.setMinSize(c2.computeSize(SWT.DEFAULT, SWT.DEFAULT));  
//				c2.layout();  
			}  
		});  
		shell.open();  
		while (!shell.isDisposed()) {  
			if (!display.readAndDispatch())  
				display.sleep();  
		}  
		display.dispose();  
	}  
	
	/*void createPage2() {
		Composite composite = new Composite(getContainer(), SWT.NONE);
		final FormLayout layout = new FormLayout();
		layout.marginWidth=3;
		layout.marginHeight=3;
		final FormData data = new FormData();
		final FormData canvasData = new FormData();
		data.left=new FormAttachment(0,5);
		data.right=new FormAttachment(100,-5);
		composite.setLayout(layout);
		final ToolBar tb = new ToolBar(composite,SWT.FLAT|SWT.WRAP);
		tb.setLayoutData(data);
		canvasData.top=new FormAttachment(tb);
		canvasData.left=new FormAttachment(0,5);
		canvasData.bottom=new FormAttachment(100,-5);
		canvasData.right=new FormAttachment(100,-5);
		final Point origin=new Point(0,0);
		final Rectangle bounds=new Rectangle(0,0,1400,1400);
		final Canvas canvas = new Canvas (composite, SWT.NO_BACKGROUND |
		SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
		final ScrollBar hBar = canvas.getHorizontalBar ();
		hBar.addListener (SWT.Selection, new Listener () {
		public void handleEvent (Event e) {
		int hSelection = hBar.getSelection ();
		int destX = -hSelection - origin.x;
		Rectangle rect = bounds;
		canvas.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
		origin.x = -hSelection;
		}
		});
		final ScrollBar vBar = canvas.getVerticalBar ();
		vBar.addListener (SWT.Selection, new Listener () {
		public void handleEvent (Event e) {
		int vSelection = vBar.getSelection ();
		int destY = - vSelection - origin.y;
		Rectangle rect =RepositoryWidgetQueue.getParent().getDisplay().getBounds();
		System.out.println(destY);
		canvas.scroll (0, destY, 0, origin.y, rect.width, rect.height, false);
		origin.y = -vSelection;
		}
		});
		canvas.addListener (SWT.Resize, new Listener () {
		public void handleEvent (Event e) {
		Rectangle rect = bounds;
		Rectangle client = canvas.getClientArea ();
		hBar.setMaximum (rect.width);
		vBar.setMaximum (rect.height);
		hBar.setThumb (Math.min (rect.width, client.width));
		vBar.setThumb (Math.min (rect.height, client.height));
		int hPage = rect.width - client.width;
		int vPage = rect.height - client.height;
		int hSelection = hBar.getSelection ();
		int vSelection = vBar.getSelection ();
		if (hSelection >= hPage) {
		if (hPage <= 0) hSelection = 0;
		origin.x = -hSelection;
		}
		if (vSelection >= vPage) {
		if (vPage <= 0) vSelection = 0;
		origin.y = -vSelection;
		}
		canvas.redraw ();
		}
		});
		canvas.setLayoutData(canvasData);
		canvas.setVisible(true);
		final GC gc = new GC(canvas);
		RepositoryWidgetQueue.setGC(gc);
		RepositoryWidgetQueue.setParent(canvas);
		final RepositoryWidgetListener rwl = new RepositoryWidgetListener();
		canvas.addMouseListener(rwl);
		canvas.addMouseMoveListener(rwl);
		tb.setVisible(true);
		tb.addListener(SWT.MouseDown, rwl);
		canvas.addPaintListener(new PaintListener () {
		public void paintControl (PaintEvent event) {
		final Rectangle area=RepositoryWidgetQueue.getParent().getClientArea();
		RepositoryWidgetQueue.draw(area.x,area.y,area.width,area.height);
		}
		});
		int index = addPage(composite);
		setPageText(index, "GUI editor [Architectures view]");
		}*/
	
}  